package com.netease.nim.uikit.common.media.imagepicker.adapter.vh;

import android.Manifest;
import androidx.core.app.ActivityCompat;
import android.view.View;
import android.view.ViewGroup;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.common.adapter.BaseViewHolder;
import com.netease.nim.uikit.common.media.imagepicker.Constants;
import com.netease.nim.uikit.common.media.imagepicker.ImagePicker;
import com.netease.nim.uikit.common.media.imagepicker.ImagePickerLauncher;
import com.netease.nim.uikit.common.media.imagepicker.camera.CaptureActivity;
import com.netease.nim.uikit.common.media.imagepicker.ui.ImageBaseActivity;

import java.util.List;


/**
 */

public class CameraViewHolder extends BaseViewHolder<Object> {

    private final ImagePicker imagePicker;
    private final ImageBaseActivity activity;

    public CameraViewHolder(ViewGroup parent, ImageBaseActivity activity, ImagePicker picker) {
        super(parent, R.layout.nim_adapter_image_list_camera);
        this.imagePicker = picker;
        this.activity = activity;
    }

    @Override
    public void findViews() {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imagePicker.videoOnly()) {
                    CaptureActivity.RECORD_MAX_TIME = imagePicker.getOption().getMaxVideoDuration();
                    CaptureActivity.RECORD_MIN_TIME = 1;

                    CaptureActivity.start(activity);
                    return;
                } else if (imagePicker.imageOnly()) {
                    XXPermissions.with(activity)
                            .constantRequest() //可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                            .permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA) //不指定权限则自动获取清单中的危险权限
                            .request(new OnPermission() {
                                @Override
                                public void hasPermission(List<String> granted, boolean isAll) {
                                    ImagePickerLauncher.takePicture(activity, Constants.REQUEST_CODE_TAKE, imagePicker.getOption());
                                }

                                @Override
                                public void noPermission(List<String> denied, boolean quick) {
                                    ToastUtils.show("请允许电报组获取权限，否则无法使用相关功能");
                                    activity.finish();
                                }
                            });
//                    if (!(activity.checkPermission(Manifest.permission.CAMERA))) {
//                        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA},
//                                                          Constants.REQUEST_PERMISSION_CAMERA);
//                    } else {
//                        ImagePickerLauncher.takePicture(activity, Constants.REQUEST_CODE_TAKE, imagePicker.getOption());
//                    }
                } else {
                    CaptureActivity.start(activity, Constants.RESULT_CODE_RECORD_VIDEO);
                }
            }
        });
    }

    @Override
    protected void onBindViewHolder(Object data) {

    }
}
